package com.mjk.common.tools.monitor.conf;


import com.mjk.common.tools.monitor.impl.DefaultMonitor;
import com.mjk.common.tools.monitor.impl.NacosMonitor;
import com.mjk.common.tools.monitor.impl.ZookepperMonitor;
import com.mjk.common.util.ObjectUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 监控组件注册类
 * @author Administrator
 *
 */
@Configuration
public class MonitorConf implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {
	BeanDefinitionRegistry registry;
	private String zkAddr = "";
    private Boolean nacosDiscovery = false;
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		if(ObjectUtils.isNotEmpty(zkAddr)) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ZookepperMonitor.class);
			builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
	        ConstructorArgumentValues values = new ConstructorArgumentValues();
	        values.addIndexedArgumentValue(0, zkAddr);
	        values.addIndexedArgumentValue(1, 3000);
	        beanDefinition.setConstructorArgumentValues(values);
            registry.registerBeanDefinition(ZookepperMonitor.class.getSimpleName(), beanDefinition);
        } else if (nacosDiscovery) {//是否开启nacos监控中心
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(NacosMonitor.class);
            builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
            registry.registerBeanDefinition(NacosMonitor.class.getSimpleName(), beanDefinition);
        } else {//注册一个空得
			BeanDefinitionBuilder builder= BeanDefinitionBuilder.rootBeanDefinition(DefaultMonitor.class);
			builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
			registry.registerBeanDefinition(DefaultMonitor.class.getSimpleName(), beanDefinition);
		}
	}

	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		this.registry = registry;
	}

	@Override
	public void setEnvironment(Environment environment) {
        zkAddr = environment.getProperty("bee.zk.address");
        nacosDiscovery = environment.getProperty("spring.cloud.nacos.discovery.enabled", Boolean.class);//nacos是否开启
	}

}
